import prisma from './prisma.service';
import { User, UserStatus, Gender } from '@prisma/client';

class UserService {
    createUser(data: {
        userId: string;
        nickname: string;
        email: string;
        password?: string;
        signature?: string;
        area?: string;
        gender?: Gender;
    }) {
        return prisma.user.create({ data });
    }

    findUserById(id: number) {
        return prisma.user.findUnique({ where: { id } });
    }

    findUserByUserId(userId: string) {
        return prisma.user.findUnique({ where: { userId } });
    }

    findUserByEmail(email: string) {
        return prisma.user.findUnique({ where: { email } });
    }

    updateUser(
        id: number,
        data: {
            nickname?: string;
            signature?: string;
            area?: string;
            gender?: Gender;
            status?: UserStatus;
            lastLogin?: Date;
        }
    ): Promise<User> {
        return prisma.user.update({
            where: { id },
            data
        });
    }


    deleteUser(userId: string): Promise<User> {
        return prisma.user.delete({
            where: { userId }
        })
    }

    async getUserFriends(userId: string): Promise<User[]> {
        const relations = await prisma.userFriend.findMany({
            where: { userId },
            include: { friend: true }
        });
        return relations.map(relation => relation.friend);
    }

    async logout(userId: string) {
        return prisma.user.update({
            where: { userId },
            data: { lastLogin: new Date() }
        })
    }
}

const userService = new UserService();
export default userService;